package com.imy.accessibility;



import android.accessibilityservice.AccessibilityService;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;

import androidx.appcompat.app.AppCompatActivity;

import com.imy.common.util.ALog;
import com.imy.common.util.ToastUtil;
import com.imy.lua.LuaCore;
import com.imy.lua.view.floating.FloatingManger;
import com.imy.touch.TouchCore;
import com.imy.touch.interfaces.CallAccessibilityService;

public class CoreService extends NodeInfoAccessibilityService implements CallAccessibilityService {
    private final String TAG = getClass().getName();
    private Handler mHandler= new Handler(Looper.getMainLooper());
    public static CoreService mService;


    @Override
    public void onCreate() {
        super.onCreate();
        ALog.i("CoreService onCreate");
        //注册lua脚本服务
        ToastUtil.toast(getBaseContext(),"Lua无障碍开启成功");
        LuaCore.get().setCallAccessibilityServiceListener(this);
        createNotify();
        ALog.i("CoreService onCreate after");
//        mService=this;
        TouchCore.get().setCallAccessibilityService(this);
    }

    @Override
    public void unbindService(ServiceConnection conn) {
        super.unbindService(conn);
        ALog.i(TAG,"CoreService： unbindService");
    }

    @Override
    public boolean onUnbind(Intent intent) {
        ALog.i(TAG,"CoreService： onUnbind");
        return super.onUnbind(intent);

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        ALog.i(TAG,"CoreService： onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }

    private void createNotify() {
        createNotifyChannel();
        startForeground(1, getBuild());
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        FloatingManger.get().setOri(newConfig.orientation);

    }

    private Notification getBuild() {
        Notification.Builder builder;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder = new Notification.Builder(this, channelId);
        } else {
            builder = new Notification.Builder(this);
        }
        String text = "为保证脚本正常运行，请勿关闭此通知栏";

        builder.setContentTitle("AutoALua")
            .setContentText(text)
            .setWhen(System.currentTimeMillis())
            .setAutoCancel(true)
            .setVibrate(new long[0])
            .build();
        return builder.build();
    }


    private String channelId = "auto_android_lua";
    private void createNotifyChannel() {
        String channelName = "AutoALua";
        int importance = 0;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            importance = NotificationManager.IMPORTANCE_HIGH;
            createNotificationChannel(channelId, channelName, importance);
        }
    }
    private void createNotificationChannel(String channelId, String channelName, int importance) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
            NotificationManager notificationManager = (NotificationManager) getSystemService(
                NOTIFICATION_SERVICE);
            // 设置通知出现时不震动
            channel.enableVibration(false);
            channel.setVibrationPattern(new long[] { 0 });
            if (notificationManager != null) {
                notificationManager.createNotificationChannel(channel);
            }
        }
    }

    //初始化
    @Override
    protected void onServiceConnected() {
        super.onServiceConnected();
        ALog.i("CoreService onServiceConnected");
        mService = this;
    }


    @Override
    public void onInterrupt() {
        ALog.i("onInterrupt");
        mService = null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ALog.i("CoreService onDestroy");
        mService = null;
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 公共方法
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    /**
     * 辅助功能是否启动
     */
    public static boolean isStart() {
        return mService != null;
    }


    public void setActivity(AppCompatActivity mainActivity) {
        activity=mainActivity;
    }
    private AppCompatActivity activity;

    @Override
    protected AppCompatActivity getActivity() {
        return activity;
    }

    @Override
    public AccessibilityService getService() {
        return this;
    }
}